package com.vogue.flowmeter.ui.adapter;

import android.bluetooth.BluetoothDevice;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.Serializable;

import no.nordicsemi.android.support.v18.scanner.ScanResult;

/**
 * 蓝牙对象拓展
 */
@SuppressWarnings("WeakerAccess")
public class ExtendedBluetoothDevice implements Serializable {
	static final int NO_RSSI = -1000;
	private final BluetoothDevice device;
	/** 一些Android设备没有解析这个名字，比如Sony Xperia Z1和Android 4.3（C6903）。它需要手动解析。*/
	public String name;
	public int rssi;
	public boolean isBonded;

	@NonNull
	public BluetoothDevice getDevice() {
		return device;
	}

	@NonNull
	public String getAddress() {
		return device.getAddress();
	}

	@Nullable
	public String getName() {
		return name;
	}

	@SuppressWarnings("WeakerAccess")
	public int getRssi() {
		return rssi;
	}

	public ExtendedBluetoothDevice(@NonNull final ScanResult scanResult) {
		this.device = scanResult.getDevice();
		this.name = scanResult.getScanRecord() != null ? scanResult.getScanRecord().getDeviceName() : null;
		this.rssi = scanResult.getRssi();
		this.isBonded = false;
	}

	public ExtendedBluetoothDevice(@NonNull final BluetoothDevice device) {
		this.device = device;
		this.name = device.getName();
		this.rssi = NO_RSSI;
		this.isBonded = true;
	}

	public boolean matches(@NonNull final ScanResult scanResult) {
		return device.getAddress().equals(scanResult.getDevice().getAddress());
	}


	@Override
	public String toString() {
		return "ExtendedBluetoothDevice{" +
				"name='" + name + '\'' +
				", rssi=" + rssi +
				", isBonded=" + isBonded +
				'}';
	}
}
